<template>
  <el-button @click="submit" :size="size" :type="type" v-bind="$attrs" v-on="$listeners"><slot></slot></el-button>
</template>

<script>
export default {
  data() {
    return {
      loading:true,
    }
  },
  props: {
    flow_code: { type: String, default: null },
    data_id: { type: String, default: null },
    nodeins_id: { type: String, default: null },

    size: { type: String, default: 'small' },
    type: { type: String, default: 'success' },
  },
  created() {},
  mounted() {
    this.load();
  },
  methods: {
    async load() {
      this.loading = true;
      this.loading = false;
    },
    async submit(){
      this.loading = true;

      if(this.em(this.flow_code) || this.em(this.data_id)){
        this.$message({ type: 'success', message: '缺少数据' });
        this.loading = false;
        return;
      }

      this.$emit('before_submit', this.v_go_submit);
    },

    submitWithDynamicUserId(dynamic_node_id,dynamic_user_id){
      this.$emit('set_loading', true);

      let dynamicData = {
        'dynamic_node_id':dynamic_node_id,
        'dynamic_user_id':dynamic_user_id,
      };
      this.v_go_submit('success',dynamicData);
    },

    async v_go_submit (flag='success',dynamicData=null){
      if(flag=='wrong'){
        this.loading = false;
        return;
      }

      this.loading = true;

      let result = '';
      if(this.em(this.nodeins_id)){//若无节点实例ID，那么发起流程
        let pData = {'flow_code':this.flow_code,'data_id':this.data_id};
        pData['token'] = elm.info.token;
        //pData['no_auto_next'] = '1';//测试用，阻止连续自动完成
        let data = await elm.post('/system/login/flow/InitFlow', pData);

        if(data.data.go_next_result!=null && data.data.go_next_result.message=='needDynamicUser'
            && data.data!=null && !this.em(data.data.data.first_nodeins_id)){//需动态指定后续节点参与者的情况
          this.$emit('set_nodeins_id', data.data.data.first_nodeins_id);

          let needDynamicUserNodeList = data.data.go_next_result.data;
          this.needDynamicUser(needDynamicUserNodeList);

          result='need_wait_and_refresh';
        }
        else{
          if(data.status=='success'){
            if(data.data!=null && data.data.go_next_result!=null && data.data.go_next_result.message == 'flowOver'){
              this.$message({ type: 'success', message: '提交成功，流程已结束' });
            }
            else if(data.data.go_next_result.message=='noNextNodeUser'){ this.$message.error(''+data.data.go_next_result.data+'节点没有找到办理用户，请检查节点参与者配置。'); }
            else{this.$message({ type: 'success', message: '已发起流程并提交至下一环节' });}

            result='success';
          }
          else {
            if(data.message=='noNextNode'){ this.$message.error('缺少后续节点。'); }
            else if(data.message=='noNextNodeUser'){ this.$message.error('【'+data.data+'】：该节点无办理用户。'); }
            else{ this.$message.error('提交失败，请稍后尝试。'); }

            result='failure';
          }
        }
      }
      else{//完成流程节点
        let pData = {'nodeins_id':this.nodeins_id};
        pData['token'] = elm.info.token;

        if(dynamicData!=null){pData['dynamic_node_id'] = dynamicData.dynamic_node_id;pData['dynamic_user_id'] = dynamicData.dynamic_user_id;}
        let data = await elm.post('/system/login/flow/CompleteNodeins', pData);
        //console.log('submit_completeNodeins: '+JSON.stringify(data));

        if(data.status=='success'){
          if(data.message == 'flowOver'){
            this.$message({ type: 'success', message: '提交成功，流程已结束' });
          }
          else{this.$message({ type: 'success', message: '已提交至下一环节' });}

          result='success';
        }
        else {
          if(data.message=='needDynamicUser'){//需动态指定后续节点参与者的情况
            let needDynamicUserNodeList = data.data;
            this.needDynamicUser(needDynamicUserNodeList);

            result='need_wait';
          }
          else{
            if(data.message=='noNextNode'){ this.$message.error('缺少后续节点。'); }
            else if(data.message=='noNextNodeUser'){ this.$message.error('【'+data.data+'节点没有找到办理用户，请检查节点参与者配置。'); }
            else{ this.$message.error('提交失败，请稍后尝试。'); }

            result='failure';
          }
        }
      }

      this.$emit('after_submit', result);

      this.loading = false;
    },

    needDynamicUser:function(needDynamicUserNodeList){
      let data  ={
        'needDynamicUserNodeList':needDynamicUserNodeList,
      };
      elm.dialog.open(this, '/system/flow/one_road_flowsec_form_s_set_user', '选择节点参与者', 700, 500, data, false);
    },

    em: function (s) {
      if (s == null || s == '') { return true; }
      else { return false; }
    },
  }
}
</script>